package com.dingding.special.order.appoint.delayappoint.impl;

import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.special.order.appoint.biz.IAppointOrderService;
import com.dingding.special.order.appoint.biz.IAppointSpecialOrderService;
import com.dingding.special.order.appoint.delayappoint.IDelayOrderHolder;

/**
 * @Description 延时指派订单的操作者
 * @author fengshuonan
 * @date 2017年1月5日 下午1:46:57
 */
@Component("delayOrderHolder")
public class DelayOrderHolder implements IDelayOrderHolder {
	
	private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
	
	
	@Override
	public void appointOrder(TimerTask task){
		executor.schedule(task, NORMAL_TIME, TimeUnit.MILLISECONDS);
	}
	
	@Override
	public void appointDelayOrder(TimerTask task) {
		executor.schedule(task, DELAY_SECONDS, TimeUnit.SECONDS);
	}
	
	public static TimerTask createTimerTask(
			final IQueueFacade queueFacade,
			final IAppointSpecialOrderService appointRealOrderService,
			final UserspecialorderT userspecialorderT){
		return new TimerTask() {
			@Override
			public void run() {
				boolean isFinish = appointRealOrderService.appointSpecialOrder(userspecialorderT);
				if(!isFinish){
					queueFacade.addSpecialOrderToQueue(userspecialorderT);
				}
			}
		};
	}
	
	public static TimerTask createTimerTask(
			final IQueueFacade queueFacade,
			final IAppointOrderService appointRealTaxiOrderService,
			final UserTaxiOrdert taxiOrdert){
		return new TimerTask() {
			@Override
			public void run() {
				boolean isFinish = appointRealTaxiOrderService.appointOrder(taxiOrdert);
				if(!isFinish){
					queueFacade.addTaxiOrderToQueue(taxiOrdert);
				}
			}
		};
	}
	
}
